<template>
    <a-card>
        <a-spin :spinning="loading">
            <a-row>
                <a-col :span="17">
                  <div v-for="(item,index) in nodeComponents.top"
                        :key="item.groupId"
                        :tab="item.groupName">
                    <component v-for="(comItem) in item.funcList" :key="comItem.funcComp" :is="comItem.funcComp"
                              :ref="comItem.funcComp"
                              :params="routeQuery"
                              :disabled="readOnly"
                              @contentTypeChange="contentTypeChange"
                              @insert="insert"></component>
                  </div>
                    <a-tabs type="card" class="my-tabs">
                        <a-tab-pane forceRender v-for="(item,index) in nodeComponents.left" :key="item.groupId"
                                    :tab="item.groupName">
                            <template v-for="(funcItem) in item.funcList">
                                <InterviewInitialDraft v-if="funcItem.funcComp==='InterviewInitialDraft'"
                                                       ref="InterviewInitialDraft"
                                                       :params="routeQuery"></InterviewInitialDraft>
                                <Editor v-else-if="funcItem.funcComp==='Editor365'||funcItem.funcComp==='EditorM'"
                                        :editor-type="funcItem.funcComp"
                                        :content-type="contentType"
                                        :readOnly="readOnly"
                                        :errors='jiaozhenResult'
                                        ref="Editor" :params="routeQuery"></Editor>
                                <component v-else :is="funcItem.funcComp" :params="routeQuery"></component>
                            </template>
                        </a-tab-pane>
                    </a-tabs>
                </a-col>
                <a-col :span="7">
                    <a-tabs
                            default-active-key="1"
                            tab-position="top">
                        <a-tab-pane :forceRender="forceRender(item.groupName)"
                                    v-for="(item,index) in nodeComponents.right"
                                    :key="item.groupId"
                                    :tab="item.groupName">
                            <component v-for="(comItem) in item.funcList" :key="comItem.funcComp" :is="comItem.funcComp"
                                       :ref="comItem.funcComp"
                                       :params="routeQuery"
                                       :disabled="readOnly"
                                       @contentTypeChange="contentTypeChange"
                                       @insert="insert"></component>
                        </a-tab-pane>
                    </a-tabs>
                    <div style="text-align: center;margin-top: 15px;">
                        <a-button v-for="btnItem in buttons" :key="btnItem.funcCode" type="primary" class="btn-item" @click="clickButton(btnItem.funcComp)">
                            {{btnItem.funcName}}
                        </a-button>

                        <a-button v-if="contentType === 1" type="primary" class="btn-item" @click="jiaozhen">新华较真</a-button></div>
                        <div style="text-align: center;margin-top: 15px;">
                          <right-content-prompt-view v-if='rightPromptShow' :errors='jiaozhenResult'/>
<!--                        <border-box title="新华较真">-->
<!--                          <a-row type="flex" class="item" v-for="(item, index) in jiaozhenResult" :key="index">-->
<!--                            <a-col v-bind="labelCol" class="label">{{-->
<!--                              index +-->
<!--                                1 +-->
<!--                                '.' +-->
<!--                                item.errMsg +-->
<!--                                '&nbsp;&nbsp;' +-->
<!--                                (item.errWord != null ? '&nbsp;&nbsp;' + ' 错误词：' + item.errWord : '&nbsp;&nbsp;') +-->
<!--                                (item.corWord != null ? '&nbsp;&nbsp;' + '建议词：' + item.corWord : '&nbsp;')-->
<!--                            }}</a-col>-->
<!--                          </a-row>-->
<!--                        </border-box>-->
                      </div>
                </a-col>
            </a-row>
        </a-spin>
        <j-select-user-by-dep-modal ref="selectModal"
                                    :modal-width="1250"
                                    :actionType="actionType"
                                    :multi="tableMulti"
                                    @ok="authorize"/>
        <preview ref="preview"></preview>
        <a-modal
                title="驳回原因"
                width="400px"
                :visible="showRejectModel"
                @cancel="closeRejectReason"
                @ok="reject">
            <a-textarea v-model="rejectReason" placeholder="请输入驳回原因" maxLength="200"></a-textarea>
        </a-modal>

        <a-modal
                title="选择栏目"
                width="400px"
                :visible="showLanmu"
                @cancel="closeLanmu"
                @ok="publishWebsite1">
                <a-select v-model="folderId" style="width:100%;">
                  <a-select-option v-for="(item, key) in folderList" :key="key" :value="item.key" >
                    {{item.value}}
                  </a-select-option>
                </a-select>
        </a-modal>


        <a-modal
                title="选择栏目"
                width="400px"
                :visible="showLanmuO"
                @cancel="closeLanmuO"
                @ok="publishWebsite2">
                <a-select v-model="folderId" style="width:100%;">
                  <a-select-option v-for="(item, key) in folderList2" :key="key" :value="item.key" >
                    {{item.value}}
                  </a-select-option>
                </a-select>
        </a-modal>
    </a-card>
</template>

<script>
  //任务流信息
  import JSelectUserByDepModal from '@/components/jeecgbiz/modal/JSelectUserByDepModal'
  import { getAction, postAction } from '@/api/manage'
  import InterviewForm from '@/components/xinke/InterviewForm'
  import InterviewInitialDraft from '@/components/xinke/InterviewInitialDraft'
  import TaskBaseInfo from '@/components/xinke/TaskBaseInfo'
  import TopicClue from '@/components/xinke/clue/TopicClue'
  import TopicLink from '@/components/xinke/link/TopicLink'
  import InterviewList from '@/components/xinke/InterviewList'
  import TaskFlow from '@/components/xinke/TaskFlow'
  import ContentForm from '@/components/xinke/content/ContentForm'
  import EditorM from '@/components/xinke/editor/EditorM'
  import Editor365 from '@/components/xinke/editor/Editor365'
  import ContentEdit from '@/components/xinke/content/ContentEdit'
  import AdditionalInfo from '@/components/xinke/AdditionalInfo'
  import selectPublishChannel from '@/components/xinke/selectPublishChannel'
  import selectPublishChannelHandler from '@/components/xinke/selectPublishChannelHandler'
  import ActivityContentForm from '@/components/xinke/content/ActivityContentForm'
  import MaterialStore from '@/components/xinke/material/MaterialStore'
  import Recommend from '@/components/xinke/Recommend'
  import Preview from '@/components/xinke/preview/Preview'
  import BigData from '@/components/xinke/BigData'
  import VersionCompareList from '@/components/xinke/VersionCompareList'
  import ActivityAdditionalInfo from '@/components/xinke/ActivityAdditionalInfo'
  import TaskInnerTopic from '@/components/xinke/TaskInnerTopic'
  import TaskAttach from '@/components/xinke/TaskAttach'
  import Editor from '@/components/xinke/editor/Editor'
  import Feedback from '@/components/xinke/Feedback'
  import ContentEditSeu from '@/components/xinke/content/ContentEditSeu'
  import BorderBox from '@comp/xinke/BorderBox.vue'
  import RightContentPromptView from './right-content-prompt-view.vue'

  //提交/保存草稿时需要校验的组件：表单组件必须立即渲染，否则提交的时候校验不到
  let needInvalidateForm = ['InterviewInitialDraft', 'ContentForm', 'ActivityContentForm', 'Editor',
    'AdditionalInfo', 'ActivityAdditionalInfo', 'Recommend', 'Feedback', 'selectPublishChannel', 'selectPublishChannelHandler']



  let needInvalidateFormAutoSave = [
  'InterviewInitialDraft',
  'ActivityContentForm',
  'Editor',
  'AdditionalInfo',
  'ActivityAdditionalInfo',
  'Recommend'
]

    //右侧tab需要初始化就渲染的组件
  let forceRenderTab = ['任务信息', '内容信息']
  export default {
    name: 'TaskFlowInfo',
    inject: ['closeCurrent'],
    components: {
      RightContentPromptView,
      BorderBox,
      Editor,
      Preview,
      JSelectUserByDepModal, InterviewForm, InterviewInitialDraft, TaskBaseInfo,
      TopicClue, TopicLink, InterviewList, EditorM, Editor365, TaskFlow, ContentForm,
      ContentEdit, AdditionalInfo, ActivityContentForm, selectPublishChannel, selectPublishChannelHandler,
      MaterialStore, Recommend, BigData, VersionCompareList, ActivityAdditionalInfo, TaskInnerTopic,
      TaskAttach, Feedback, ContentEditSeu
    },
    data() {
      return {
        loading: false,
        userId: '',
        routeQuery: this.$route.query,
        readOnly: this.$route.query.isHistory == '2',
        taskNo: this.$route.query.taskNo,
        nodeComponents: {},
        buttons: [],
        showRejectModel: false,
        rejectReason: '',
        contentType: undefined,
        tableMulti: false,
        actionType: undefined,
        folderId:'',
        folderList:[{
          "key":"5173","value":"东大要闻"
        },
        {
          "key":"54390","value":"东大要闻-大图新闻"
        },
        {
          "key":"54388","value":"东大要闻-图片新闻"
        },
        {
          "key":"54389","value":"东大要闻-新闻"
        },
        {
          "key":"5172","value":"媒体东大"
        },
        {
          "key":"54377","value":"媒体东大-图片新闻"
        },
        {
          "key":"5200","value":"东大人物"
        },
        {
          "key":"5215","value":"教育教学"
        },
        {
          "key":"5214","value":"科技动态"
        },
        {
          "key":"21400","value":"合作交流"
        },
        {
          "key":"5183","value":"院系传真"
        },
        {
          "key":"5217","value":"服务社会"
        },
        {
          "key":"5182","value":"菁菁校园"
        },
        {
          "key":"5177","value":"校报快览"
        },
        {
          "key":"5181","value":"电视新闻"
        },
        {
          "key":"5174","value":"头条新闻"
        },
        {
          "key":"32635","value":"首页大图"
        },
        {
          "key":"5202","value":"热点推荐"
        },
        {
          "key":"32527","value":"推荐新闻"
        },
        {
          "key":"32528","value":"平台新闻"
        },
        {
          "key":"32529","value":"东大融媒体-微信公众号"
        },
        {
          "key":"32530","value":"东大融媒体-强国号"
        },
        {
          "key":"32531","value":"东大融媒体-教育号"
        },
        {
          "key":"32522","value":"东大抖音"
        },
        {
          "key":"5186","value":"专题新闻"
        },
        {
          "key":"5220","value":"视频东大"
        }
        ,
        {
          "key":"54282","value":"学术动态"
        }
        ,
        {
          "key":"54284","value":"融媒东大"
        }
        ,
        {
          "key":"54285","value":"专题热点"
        },
        {
          "key":"54286","value":"人文东大"
        }
      ],
        folderList2:[{
          "key":"45727","value":"院系设置"
        },
        {
          "key":"45764","value":"东大要闻"
        },
        {
          "key":"45768","value":"东大要闻-图片切换"
        },
        {
          "key":"45769","value":"东大要闻-图片新闻"
        },
        {
          "key":"45770","value":"东大要闻-新闻"
        },
        {
          "key":"45766","value":"东大学人"
        },
        {
          "key":"16155","value":"角色"
        },
        {
          "key":"16156","value":"角色-教职工"
        },
        {
          "key":"16157","value":"角色-在校生"
        },
        {
          "key":"16158","value":"角色-访客"
        },
        {
          "key":"16159","value":"角色-公共服务"
        },
        {
          "key":"27061","value":"角色-电子邮箱"
        },
        {
          "key":"45771","value":"媒体东大"
        },
        {
          "key":"45772","value":"媒体东大-图片新闻"
        },
        {
          "key":"45796","value":"媒体东大-新闻"
        },
        {
          "key":"28396","value":"标识系统（新）"
        },
        {
          "key":"28397","value":"基础部分"
        },
        {
          "key":"28399","value":"基础部分-校标规范"
        },

        {
          "key":"28400","value":"基础部分-标准字规范"
        },
        {
          "key":"28401","value":"基础部分-校标与标准字组合规范"
        },
        {
          "key":"28402","value":"基础部分-色彩规范"
        },
        {
          "key":"28403","value":"基础部分-校训字体规范"
        },
        {
          "key":"28404","value":"基础部分-象征图形"
        },
        {
          "key":"28398","value":"应用部分"
        }, {
          "key":"45775","value":"图书档案"
        }, {
          "key":"45776","value":"数字东大"
        }, {
          "key":"45777","value":"站内链接"
        }, {
          "key":"45778","value":"学术东大"
        }, {
          "key":"45781","value":"简介内容"
        }, {
          "key":"45782","value":"简介内容-内容1"
        },{
          "key":"45783","value":"简介内容-内容2"
        },{
          "key":"45784","value":"简介内容-数据"
        },{
          "key":"45785","value":"简介内容-内容3"
        },{
          "key":"45786","value":"简介内容-内容4"
        },{
          "key":"45787","value":"简介内容-内容5"
        },{
          "key":"45788","value":"简介内容-内容6"
        },{
          "key":"45789","value":"简介内容-内容7"
        },{
          "key":"45795","value":"活力东大"
        },
        {
          "key":"45797","value":"招生就业"
        },
        {
          "key":"45798","value":"招生就业-本科生"
        },{
          "key":"45799","value":"招生就业-研究生"
        },{
          "key":"45800","value":"招生就业-就业办"
        },{
          "key":"45801","value":"招生就业-海外教育学院"
        },
        {
          "key":"45803","value":"融媒东大"
        },
        {
          "key":"45804","value":"融媒东大-视觉东大"
        },
        {
          "key":"26938","value":"微博"
        },
        {
          "key":"26939","value":"微信"
        },
        {
          "key":"26940","value":"抖音"
        }, {
          "key":"45644","value":"bilibili"
        },
        {
          "key":"45679","value":"二维码"
        },
        {
          "key":"26930","value":"校区地址"
        },
        {
          "key":"27083","value":"学校概况"
        },
        {
          "key":"27089","value":"学校简介"
        },
        {
          "key":"45737","value":"学校简介-简介"
        },
        {
          "key":"45738","value":"学校简介-校标"
        },
        {
          "key":"45817","value":"学校简介-校徽"
        },
        {
          "key":"45811","value":"学校简介-校庆日"
        },
        {
          "key":"45818","value":"学校简介-校训"
        },
        {
          "key":"45814","value":"学校简介-校歌"
        },{
          "key":"45815","value":"学校简介-校标2"
        },{
          "key":"45819","value":"学校简介-内容"
        },
        {
          "key":"27090","value":"历史沿革"
        },
        {
          "key":"27091","value":"历任领导"
        },{
          "key":"27094","value":"历任领导-历任领导信息"
        },
        {
          "key":"45748","value":"历任领导-1949年前"
        },
        {
          "key":"45750","value":"历任领导-1949年前-三江师范学堂"
        },
        {
          "key":"45751","value":"历任领导-1949年前-两江师范学堂"
        },
        {
          "key":"45752","value":"历任领导-1949年前-南京高等师范学校"
        },
        {
          "key":"45753","value":"历任领导-1949年前-国立东南大学"
        },
        {
          "key":"45754","value":"历任领导-1949年前-国立第四中山大学"
        },
        {
          "key":"45755","value":"历任领导-1949年前-江苏大学"
        },
        {
          "key":"45756","value":"历任领导-1949年前-国立中央大学"
        },
        {
          "key":"45749","value":"历任领导-1949年后"
        },
        {
          "key":"45757","value":"历任领导-1949年后-国立南京大学"
        },
        {
          "key":"45758","value":"历任领导-1949年后-南京大学"
        },
        {
          "key":"45759","value":"历任领导-1949年后-南京工学院"
        },
        {
          "key":"45760","value":"历任领导-1949年后-东南大学"
        },
        {
          "key":"27092","value":"历任领导-现任领导"
        },
        {
          "key":"27095","value":"历任领导-现任领导-现任领导信息"
        },
        {
          "key":"27093","value":"历任领导-组织机构"
        },
        {
          "key":"27124","value":"历任领导-标识系统"
        },
        {
          "key":"27128","value":"历任领导-标识系统-标准字规范"
        },
        {
          "key":"27129","value":"历任领导-标识系统-辅助图形规范"
        },
        {
          "key":"27130","value":"历任领导-标识系统-色彩规范"
        },
        {
          "key":"27131","value":"历任领导-标识系统-校标规范"
        },
        {
          "key":"27132","value":"历任领导-标识系统-校标与标准字组合规范"
        },
        {
          "key":"27133","value":"历任领导-标识系统-校训字体规范"
        },
        {
          "key":"39899","value":"历任领导-东大校历"
        },
        {
          "key":"27085","value":"师资力量"
        },
        {
          "key":"27096","value":"师资力量-两院院士风采"
        },
        {
          "key":"27097","value":"师资力量-研究生导师"
        },
        {
          "key":"27098","value":"师资力量-知名专家学者"
        },
        {
          "key":"27099","value":"师资力量-教师教学发展中心"
        },
        {
          "key":"27125","value":"师资力量-师资队伍描述"
        },
        {
          "key":"27086","value":"教育教学"
        },
        {
          "key":"27048","value":"科学研究"
        },
        {
          "key":"28440","value":"科学研究-科研院"
        },
        {
          "key":"27100","value":"科学研究-科研机构"
        },
        {
          "key":"27127","value":"科学研究-科研机构描述"
        },
        {
          "key":"28441","value":"科学研究-技术转移中心"
        },
        {
          "key":"27101","value":"科学研究-科学园"
        },{
          "key":"27102","value":"科学研究-资产经营有限公司"
        },
        {
          "key":"27013","value":"大图切换"
        }
        ],
        showLanmu:false,
        showLanmuO:false,
        showJiaozhenModel: false,

        jiaozhenText: '',
        jiaozhenResult: [],
        rightPromptShow: false,
      }
    },
    created() {
      if (this.routeQuery.isHistory == '2') {
        this.fetchAgreedComponents()
      } else {
        //获取当前节点的组件信息
        this.fetchComponents()
      }
    },
    mounted() {
    let that = this
    // setTimeout(() => {
      if (this.routeQuery.isHistory == '1') {
          this.interveal = window.setInterval(() => {
            if (stop) {
              that.autosave()
            }
          }, 60000)
        }
    // }, 30000)
  },
  destroyed() {
    window.clearInterval(this.interveal)
  },
    methods: {
      contentTypeChange(contentType) {
        this.contentType = contentType
      },
      forceRender(groupName) {
        return forceRenderTab.includes(groupName)
      },
      //显示约定组件信息
      fetchAgreedComponents() {
        //先查看有没有采访节点
        const nodeComponents = {
          top: [{
              groupId: 'A03',
              groupName: '内容信息',
              funcList: [{
                funcCode: '008',
                funcComp: 'ContentForm'
              }]
            }],
          left: [
            {
              groupId: 'A01',
              groupName: '富文本',
              funcList: [{
                funcCode: '002',
                funcComp: 'EditorM'
              }]
            }
          ],
          right: [
            {
              groupId: 'A04',
              groupName: '任务信息',
              funcList: [{
                funcCode: '004',
                funcComp: 'TaskBaseInfo'
              }, {
                funcCode: '018',
                funcComp: 'TaskInnerTopic'
              }, {
                funcCode: '005',
                funcComp: 'TopicClue'
              }, {
                funcCode: '006',
                funcComp: 'TopicLink'
              }]
            },
            {
              groupId: 'A02',
              groupName: '任务流转',
              funcList: [
                {
                  funcCode: '012',
                  funcComp: 'TaskFlow'
                }
              ]
            }
          ]
        }
        //如果是活动，直接富文本，如果是文章，可能有采访
        //历史数据仅查看，显示约定部分组件信息
        let contentForm
        if (this.routeQuery.taskType == '0') {
          //文章
          contentForm = {
            groupId: 'A03',
            groupName: '内容信息',
            funcList: [
              {
                funcCode: '008',
                funcComp: 'ContentForm'
              },
              {
                funcCode: '009',
                funcComp: 'AdditionalInfo'
              }
            ]
          }
          this.buttons = ['preview']
        } else {
          //活动
          contentForm = {
            groupId: 'A03',
            groupName: '内容信息',
            funcList: [
              {
                funcCode: '021',
                funcComp: 'ActivityContentForm'
              },
              {
                funcCode: '022',
                funcComp: 'ActivityAdditionalInfo'
              }
            ]
          }
        }
        nodeComponents.right.splice(1, 0, contentForm)
        this.$nextTick(() => {
          this.nodeComponents = nodeComponents
        })
      },
      //获取当前节点组件
      fetchComponents() {
        this.loading = true
        getAction('sys/workflowNode/queryNodeFuncTask', this.routeQuery).then(res => {
          if (res.success) {
            const top = [];
            const left = [];
            const right = [];
            let buttons = null
            res.result.forEach(item => {
              if (item.postion == 0) {
                left.push(item)
              } else if (item.postion == 1) {
                if (item.groupName === '内容信息') {
                  const topItem = JSON.parse(JSON.stringify(item));
                  topItem.funcList = [];
                  const rightItem = JSON.parse(JSON.stringify(item));
                  rightItem.funcList = [];
                  for (let i = 0; i < item.funcList.length; i += 1) {
                    if (item.funcList[i].funcComp === 'ContentForm') {
                      // 内容基本信息移至富文本上方显示
                      topItem.groupId = `A03${item.groupId}`;
                      topItem.funcList.push(item.funcList[i]);
                    } else {
                      rightItem.funcList.push(item.funcList[i]);
                    }
                  }
                  top.push(topItem);
                  right.push(rightItem);
                } else {
                  right.push(item);
                }
              } else {
                buttons = item.funcList
              }
            })
            this.nodeComponents = { top, left, right }
            console.log(this.nodeComponents);
            this.buttons = buttons
          } else {
            this.$message.error(res.message)
          }
        }).catch(error => console.log(error))
          .finally(() => {
            this.loading = false
          })
      },
      //插入富文本
      insert(content) {
        if (this.$refs.Editor && this.$refs.Editor[0]) {
          this.$refs.Editor[0].insert(content)
        }
      },
      includeButton(button) {
        if (this.buttons) {
          return this.buttons.includes(button)
        }
        return false
      },
      clickButton(funcComp) {
        console.log(funcComp);
        switch (funcComp) {
          case 'preview':
            this.preview()
            break
          case 'authorize':
            this.tableMulti = false
            this.showUserModal('authorize')
            break
          case 'sync':
            this.tableMulti = true
            this.showUserModal('sync')
            break
          case 'addSign':
            this.tableMulti = false
            this.showUserModal('addSign')
            break
          case 'save':
            this.save()
            break
          case 'submit':
            this.submit()
            break
          case 'pass':
            this.pass()
            break
          case 'reject':
            this.showRejectModel = true
            break
          case 'saveToContents':
            this.saveToContents();
            break
          case 'publishWebsite':
            this.publishWebsite();
            break
          case 'publishWebsiteO':
            this.publishWebsiteO();
            break
          case 'doneTask':
            this.doneTask()
          default:
            break
        }
      },
      //校验所有要校验的表单
      validateAllForm(actionUrl, callback) {
        let promises = []
        needInvalidateForm.forEach(item => {
          const refFormArr = this.$refs[item]
          if (refFormArr && refFormArr.length > 0) {
            const refForm = refFormArr[0]
            if (refForm && refForm.selfValidate) {
              promises.push(refForm.selfValidate())
            }
          }
        })
        console.log(promises);
        //所有表单校验通过
        Promise.all(promises).then(values => {
          let params = { ...this.routeQuery }
          console.log(params);
          console.log(values);
          values.forEach(item => {
            if (item.key) {
              const old = params[item.key]
              let value = item.value
              if (old) {
                value = Object.assign(old, value)
              }
              params[item.key] = value
            } else {
              Object.assign(params, item)
            }
          })
          console.log(params);
          if (callback && typeof callback === 'function') {
            callback.call(this, params)
          } else if (actionUrl) {
            this.loading = true
            postAction(actionUrl, params).then(res => {
              if (res.success) {
                this.$message.success('操作成功')
                this.closeCurrent()
              } else {
                this.$message.error(res.message)
              }
            }).catch(error => console.log(error))
              .finally(() => {
                this.loading = false
              })
          }
        }, reason => {
          this.$notification.error({
            message: '校验失败',
            description: reason || '请填写必要信息'
          })
          // this.$message.warning(reason || '请填写必要信息');
        })
      },


      validateAllFormnocolse(actionUrl, callback) {
        let promises = []
        needInvalidateForm.forEach(item => {
          const refFormArr = this.$refs[item]
          if (refFormArr && refFormArr.length > 0) {
            const refForm = refFormArr[0]
            if (refForm && refForm.selfValidate) {
              promises.push(refForm.selfValidate())
            }
          }
        })
        console.log(promises);
        //所有表单校验通过
        Promise.all(promises).then(values => {
          let params = { ...this.routeQuery }
          console.log(params);
          console.log(values);
          values.forEach(item => {
            if (item.key) {
              const old = params[item.key]
              let value = item.value
              if (old) {
                value = Object.assign(old, value)
              }
              params[item.key] = value
            } else {
              Object.assign(params, item)
            }
          })
          console.log(params);
          if (callback && typeof callback === 'function') {
            callback.call(this, params)
          } else if (actionUrl) {
            this.loading = true
            postAction(actionUrl, params).then(res => {
              if (res.success) {
                this.$message.success('操作成功')
                // this.closeCurrent()
              } else {
                this.$message.error(res.message)
              }
            }).catch(error => console.log(error))
              .finally(() => {
                this.loading = false
              })
          }
        }, reason => {
          this.$notification.error({
            message: '校验失败',
            description: reason || '请填写必要信息'
          })
          // this.$message.warning(reason || '请填写必要信息');
        })
      },


    validateAllFormForSave(actionUrl) {
      let promises = []
      // let needInvalidateFormNew = needInvalidateForm
      // needInvalidateFormNew.splice(1,1)
      // console.log(needInvalidateFormNew)
      needInvalidateFormAutoSave.forEach(item => {
        const refFormArr = this.$refs[item]
        // console.log(refFormArr)

        if (refFormArr && refFormArr.length > 0) {
          const refForm = refFormArr[0]
          if (refForm && refForm.selfValidate) {
            promises.push(refForm.selfValidate())
          }
        }
      })
      //所有表单校验通过
      Promise.all(promises).then(
        values => {
          let params = { ...this.routeQuery }
          values.forEach(item => {
            if (item.key) {
              const old = params[item.key]
              let value = item.value
              if (old) {
                value = Object.assign(old, value)
              }
              params[item.key] = value
            } else {
              Object.assign(params, item)
            }
          })
          if (actionUrl) {
            // this.loading = true
            // params.contentTitle = this.$refs.ContentForm[0].form.contentTitle
            // params.mbLogoUrl = this.$refs.ContentForm[0].form.mbLogoUrl
            postAction(actionUrl, params)
              .then(res => {
                if (res.success) {
                  this.$message.success('自动保存成功')
                  // this.closeCurrent()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(error => console.log(error))
              .finally(() => {
                // this.loading = false
              })
          }
        },
        reason => {
          this.$notification.error({
            message: '校验失败',
            description: reason || '请填写必要信息'
          })
          // this.$message.warning(reason || '请填写必要信息');
        }
      )
    },


      //自动保存草稿
    autosave() {
      this.validateAllFormForSave('task/taskInfo/autoSaveTask')
    },


      //保存草稿
      save() {
        this.validateAllFormnocolse('task/taskInfo/saveTask')
      },
      //提交
      submit() {
        this.validateAllForm('task/taskInfo/processTask')
      },
      //预览
      preview() {
        //实时取内容信息
        const ele = (this.$refs.ContentForm && this.$refs.ContentForm[0]) || (this.$refs.ActivityContentForm && this.$refs.ActivityContentForm[0])
        if (ele) {
          const info = ele.getContentInfo()
          if (this.$refs.Editor && this.$refs.Editor[0]) {
            const richContent = this.$refs.Editor[0].getRichContent()
            Object.assign(info, richContent)
          }
          if (this.$refs.PublishTime && this.$refs.PublishTime[0]) {
            info.publish = this.$refs.PublishTime[0].getFormInfo()
          }
          this.$refs.preview.show(info)
        } else {
          this.$message.error('无法预览')
        }
      },
      showUserModal(actionType) {
        this.$refs.selectModal.showModal()
        this.actionType = actionType
      },
      // 授权
      authorize(rows, idstr) {
        if (this.actionType === 'sync') {
          const arr = idstr.split(',');
          const params = {
            taskNo: this.taskNo,
            id: arr
          }
          const url = '/task/taskInfo/notifyAndSync'
            postAction(url, params).then(res => {
              if (res.success) {
                this.$message.success('同步通知成功')
              } else {
                this.$message.error(res.message)
              }
            }).catch(error => console.log(error))
        } else {
          this.validateAllForm(null,()=>{
            this.loading = true
            const params = {
              ...this.routeQuery,
              grantUserId: rows[0].id,
              grantUserName: rows[0].realname == '' ?rows[0].username:rows[0].realname
            }
            console.log(params);
            const url = this.actionType === 'authorize' ? 'task/taskInfo/grantTask' : 'task/taskInfo/grantTaskBoost'
            getAction(url, params).then(res => {
              if (res.success) {
                this.closeCurrent()
              } else {

              }
            }).catch(error => console.log(error))
              .finally(() => {
                this.loading = false
              })
          })
        }
      },
      //审核通过:与提交一个接口，只是不传表单参数
      pass() {
        this.validateAllForm(null,()=>{
          this.loading = true
          postAction('task/taskInfo/processTask', this.routeQuery).then(res => {
            if (res.success) {
              this.$message.success('审核成功')
              this.closeCurrent()
            } else {
              this.$message.error(res.message)
            }
          }).catch(error => console.log(error))
            .finally(() => {
              this.loading = false
            })
        })
      },
      //驳回
      reject() {
        this.validateAllForm(null,()=>{
          const remark = this.rejectReason
          this.closeRejectReason()
          this.loading = true
          getAction('task/taskInfo/rejectTask', { ...this.routeQuery, remark }).then(res => {
            if (res.success) {
              this.closeCurrent()
            } else {
              this.$message.error(res.message)
            }
          }).catch(error => console.log(error))
            .finally(() => {
              this.loading = false
            })
        });
      },
      jiaozhen() {
        const richContent = this.$refs.Editor[0].getRichContent()
        this.loading = true
        console.log(richContent.richContent)
        // this.showJiaozhenModel = true
        postAction('jiaozhen/jiaozhen/jiaozhen2', { content: richContent.richContent })
          .then(res => {
            if (res.success) {
              this.jiaozhenResult = res.result
              // this.$message.success(res.result);
            } else {
              this.$message.error(res.message)
            }
          }).catch(error => console.log(error))
          .finally(() => {
            this.rightPromptShow = true
            this.loading = false
          })
      },



      closeRejectReason() {
        this.showRejectModel = false
        this.rejectReason = ''
      },
      //  保存到内容库
      saveToContents() {
        this.validateAllForm(null, (params) => {
          this.loading = true
          let templateParams = {
            childNodeId: params.childNodeId,
            content: params.richContent,
            taskNo: params.taskNo,
            taskType: params.taskType
          }
          Object.assign(templateParams, params.textContentInfo)
          templateParams.pcLogoUrl = templateParams.mbLogoUrl
          delete templateParams.mbLogoUrl
          postAction('task/taskInfo/saveConContentBankInter', templateParams).then(res => {
            if (res.success) {
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
          }).finally(_ => {
            this.loading = false
          })
        })
      },
      publishWebsite(){
          this.showLanmu = true
      },
      publishWebsiteO(){
          this.showLanmuO = true
      },
      closeLanmu(){
        this.showLanmu = false
      },
      closeLanmuO(){
        this.showLanmuO = false
      },
      // 发布新闻网
      publishWebsite1() {
        // alert(this.folderId)
        this.validateAllForm(null,()=>{
          this.loading = true
          // postAction('task/taskInfo/processTask', this.routeQuery).then(res => {
          //   if (res.success) {
          //     this.$message.success('任务处理完成')

              const publishValidate = ['ContentForm', 'Editor','AdditionalInfo'];
              let promises = []
              publishValidate.forEach(item => {
                const refFormArr = this.$refs[item]
                if (refFormArr && refFormArr.length > 0) {
                  const refForm = refFormArr[0]
                  if (refForm && refForm.selfValidate) {
                    promises.push(refForm.selfValidate())
                  }
                }
              })
              Promise.all(promises).then(values => {
                let params = { ...this.routeQuery }
                values.forEach(item => {
                  if (item.key) {
                    const old = params[item.key]
                    let value = item.value
                    if (old) {
                      value = Object.assign(old, value)
                    }
                    params[item.key] = value
                  } else {
                    Object.assign(params, item)
                  }
                })
                console.log(params)
                const publishParams = {
                  contentNo: params.textContentInfo.contentNo,
                  contentTitle: params.textContentInfo.contentTitle,
                  content: params.richContent,
                  banner: params.textContentInfo.mbLogoUrl,
                  folderId: this.folderId,
                  taskNo: params.taskNo,
                  contentAddition: params.contentAddition,
                  contentIntroduce: params.textContentInfo.contentIntroduce
                };
                postAction('third/news/add', publishParams).then(res => {
                  if(res.success){
                    this.$message.success("发布新闻网成功");
                    // this.closeCurrent()
                    this.loading =false
                    this.closeLanmu()
                  } else {
                    this.$message.error(res.message);
                    this.closeLanmu()

                  }
                });
              }, reason => {
                this.$notification.error({
                  message: '校验失败',
                  description: reason || '请填写必要信息'
                })
              })
            // } else {
            //   this.$message.error(res.message)
            // }
          // })
        })

      },
      //发布官网
      publishWebsite2() {
        // alert(this.folderId)
        this.validateAllForm(null,()=>{
          this.loading = true

              const publishValidate = ['ContentForm', 'Editor','AdditionalInfo'];
              let promises = []
              publishValidate.forEach(item => {
                const refFormArr = this.$refs[item]
                if (refFormArr && refFormArr.length > 0) {
                  const refForm = refFormArr[0]
                  if (refForm && refForm.selfValidate) {
                    promises.push(refForm.selfValidate())
                  }
                }
              })



              Promise.all(promises).then(values => {
                let params = { ...this.routeQuery }
                values.forEach(item => {
                  if (item.key) {
                    const old = params[item.key]
                    let value = item.value
                    if (old) {
                      value = Object.assign(old, value)
                    }
                    params[item.key] = value
                  } else {
                    Object.assign(params, item)
                  }
                })
                console.log(params)
                const publishParams = {
                  contentNo: params.textContentInfo.contentNo,
                  contentTitle: params.textContentInfo.contentTitle,
                  content: params.richContent,
                  banner: params.textContentInfo.mbLogoUrl,
                  folderId: this.folderId,
                  domainId:2,
                  taskNo: params.taskNo,
                  contentAddition: params.contentAddition,
                  contentIntroduce: params.textContentInfo.contentIntroduce


                };
                postAction('third/news/add', publishParams).then(res => {
                  if(res.success){

                    this.$message.success("发布官网成功");
                    // this.closeCurrent()
                    this.loading =false
                    this.closeLanmu()
                  } else {
                    this.$message.error(res.message);
                    this.closeLanmu()

                  }
                });
              }, reason => {
                this.$notification.error({
                  message: '校验失败',
                  description: reason || '请填写必要信息'
                })
              })
            // } else {
            //   this.$message.error(res.message)
            // }
          // })
        })

      },
      doneTask() {
        // alert(this.folderId)
        this.validateAllForm(null,()=>{
          this.loading = true
          postAction('task/taskInfo/processTask', this.routeQuery).then(res => {
            if (res.success) {
              this.$message.success('任务处理完成')
              this.closeCurrent()
              this.loading =false

            } else {
              this.$message.error(res.message)
            }
          })
        })

      },
    }
  }
</script>

<style scoped>
    .btn-item {
        margin: 5px 5px;
    }

    /deep/ .my-tabs .ant-tabs-bar {
        border-bottom: none;
    }
</style>
